package com.qdlc.p2p.dal.model;

import java.util.Date;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import com.qdlc.p2p.common.exception.BusinessException;
import com.qdlc.p2p.common.exception.UserException;
import com.qdlc.p2p.common.util.BeanUtil;
import com.qdlc.p2p.common.util.DataUtils;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.MessageUtil;
import com.qdlc.p2p.common.util.ValidateUtil;
import com.qdlc.p2p.dal.dao.ExperienceDao;
import com.qdlc.p2p.dal.dao.ExperienceRuleDao;
import com.qdlc.p2p.dal.dao.UserDao;
import com.qdlc.p2p.dal.dto.BorrowTender;
import com.qdlc.p2p.dal.dto.Experience;
import com.qdlc.p2p.dal.dto.ExperienceRule;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.dto.UserCache;
import com.qdlc.p2p.dal.util.UserUtils;

/**
 * 体验金model
 * @author xxx
 * @version 2.0
 * @date 2015年4月23日 上午10:56:45
 */
public class ExperienceModel extends Experience {
	
	private Map<String, Object> data;
	
	/** 状态-未使用 */
	public static final byte EXPERIENCE_STATUS_UNUSED = 0;
	/** 状态-已使用 */
	public static final byte EXPERIENCE_STATUS_USED = 1;
	/** 状态-过期的 */
	public static final byte EXPERIENCE_STATUS_EXPIRED = -1;
	/** 状态-无效的 */
	public static final byte EXPERIENCE_STATUS_INVALID = 2;
	/** 状态-冻结的 */
	public static final byte EXPERIENCE_STATUS_FREEZE = 3;
	
	/** 当前页码 */
	private int page = 1;

	/** 每页数据条数 */
	private int rows = 5;
	
	/**
	 * 用户名
	 */
	private String userName; 
	/**
	 * 用户真实姓名
	 */
	private String realName; 
	
	/**
	 * 规则名称
	 */
	private String ruleName;

	/**
	 * 条件查询
	 */
	private String searchName;
	/**
	 * 领取开始时间
	 */
	private String receiveStartTime;
	/**
	 * 领取结束时间
	 */
	private String receiveEndTime;
	/**
	 * 过期开始时间
	 */
	private String expiredStartTime;
	/**
	 * 过期结束时间
	 */
	private String expiredEndTime;
	
	/**
	 * 领取开始时间
	 */
	private Date receiveStartDate;
	
	/**
	 * 领取结束时间
	 */
	private Date receiveEndDate;
	
	/**
	 * 过期开始时间
	 */
	private Date expiredStartDate;
	
	/**
	 * 过期结束时间
	 */
	private Date expiredEndDate;
	
	public static ExperienceModel instance(Experience experience) {
		ExperienceModel experienceModel = new ExperienceModel();
		BeanUtils.copyProperties(experience, experienceModel);
		return experienceModel;
	}

	public Experience prototype() {
		Experience experience = new Experience();
		BeanUtils.copyProperties(this, experience);
		return experience;
	}
	
	//*************************define for manage  start**********************************/
	/**
	 * 检查提交的数据格式
	 * 
	 * @return
	 */
	public int validModelData() {
		if (!ValidateUtil.isUserName(getUserName())) {
			throw new UserException(MessageUtil.getMessage("MF102001003"), 1);
		}
		return -1;
	}
	
	/**
	 * 添加
	 * 根据ExperienceModel及userName获得Experience
	 * @return
	 * @throws Exception
	 */
	public Experience prototypeForAppend() throws Exception {
		Experience experience = this.prototype();
		UserDao userDao = BeanUtil.getBean(UserDao.class);
		User user = userDao.findObjByProperty("userName", userName);
		if(user==null){
			throw new UserException(MessageUtil.getMessage("MF102001002"), 1);
		}
		UserCache cache = UserUtils.getUserCacheByUserId(user);
		//校验用户类型
		if (cache.getUserType()==2) {
			throw new UserException(MessageUtil.getMessage("MF102001006"), 1);
		}
		experience.setUserId(user.getUserId());
		ExperienceRuleDao experienceRuleDao = BeanUtil.getBean(ExperienceRuleDao.class);
		ExperienceRule experienceRule = experienceRuleDao.find(getExperienceRuleId());
		experience.setExperienceRuleId(experienceRule.getId());
		experience.setAmount(experienceRule.getAmount());
		experience.setStatus(ExperienceModel.EXPERIENCE_STATUS_UNUSED);
		experience.setAddTime(DateUtil.getNow());
		experience.setValidTime(experienceRule.getValidTime());
		experience.setName(experienceRule.getName());
		experience.setExpiredTime(DateUtil.rollDay(experience.getAddTime(), experience.getValidTime()));
		return experience;
	}
	
	/**
	 * 修改
	 * 根据ExperienceModel获得Experience
	 * @return
	 * @throws Exception
	 */
	public Experience prototypeForModify() throws Exception {
		Experience experience = this.prototype();
		ExperienceDao experienceDao = BeanUtil.getBean(ExperienceDao.class);
		Experience experience_ = experienceDao.find(getId());
		if(experience_.getStatus()!=0){
			throw new BusinessException(MessageUtil.getMessage("MF102002002"), 1);
		}
		experience.setUserId(experience_.getUserId());
		experience.setExperienceRuleId(experience_.getExperienceRuleId());
		experience.setAmount(experience_.getAmount());
		experience.setStatus(experience_.getStatus());
		experience.setAddTime(experience_.getAddTime());
		experience.setUseTime(experience_.getUseTime());
		experience.setValidTime(experience_.getValidTime());
		experience.setName(experience_.getName());
		experience.setSourceType(experience_.getSourceType());
		return experience;
	}
	//*************************define for manage  end**********************************/
	
	//*************************define for core  start**********************************/
	/**
	 * 校验体验券是否可以使用
	 * @param experience
	 * @return
	 */
	public boolean validForUse(BorrowTender tender, Experience experience){
		boolean result = false;
		if(experience == null){
			return result;
		}
		if(tender.getUser()!=null && tender.getUser().getUserId() != experience.getUserId()){
			return result;
		}
		if(experience.getStatus() != ExperienceModel.EXPERIENCE_STATUS_UNUSED){
			return result;
		}
		if(experience.getExpiredTime().before(DateUtil.getNow())){
			return result;
		}
		result = true;
		return result;
	}
	
	public void setStartAndEndDate(){
		if(StringUtils.isNotBlank(receiveStartTime)){
			receiveStartDate = DateUtil.valueOf(receiveStartTime);
		}
		if(StringUtils.isNotBlank(receiveEndTime)){
			receiveStartDate = DateUtil.valueOf(receiveEndTime);
		}
		if(StringUtils.isNotBlank(expiredStartTime)){
			expiredStartDate = DateUtil.valueOf(expiredStartTime);
		}
		if(StringUtils.isNotBlank(expiredEndTime)){
			expiredEndDate = DateUtil.valueOf(expiredEndTime);
		}
	}
	
	///*************************define for core  end**********************************/
	

	public Map<String, Object> getData() {
		return data;
	}

	public void setData(Map<String, Object> data) {
		this.data = data;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public String getRuleName() {
		return ruleName;
	}

	public void setRuleName(String ruleName) {
		this.ruleName = ruleName;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getRealName() {
		return realName;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	public String getSearchName() {
		return searchName;
	}

	public void setSearchName(String searchName) {
		this.searchName = searchName;
	}

	public String getReceiveStartTime() {
		return receiveStartTime;
	}

	public void setReceiveStartTime(String receiveStartTime) {
		this.receiveStartTime = receiveStartTime;
		this.receiveStartDate = DataUtils.toDate(receiveStartTime);
	}

	public String getReceiveEndTime() {
		return receiveEndTime;
	}

	public void setReceiveEndTime(String receiveEndTime) {
		this.receiveEndTime = receiveEndTime;
		this.receiveEndDate = DataUtils.toDate(receiveEndTime);
	}

	public String getExpiredStartTime() {
		return expiredStartTime;
	}

	public void setExpiredStartTime(String expiredStartTime) {
		this.expiredStartTime = expiredStartTime;
		this.expiredStartDate = DataUtils.toDate(expiredStartTime);
	}

	public String getExpiredEndTime() {
		return expiredEndTime;
	}

	public void setExpiredEndTime(String expiredEndTime) {
		this.expiredEndTime = expiredEndTime;
	}

	public Date getReceiveStartDate() {
		return receiveStartDate;
	}

	public Date getReceiveEndDate() {
		return receiveEndDate;
	}

	public Date getExpiredStartDate() {
		return expiredStartDate;
	}

	public Date getExpiredEndDate() {
		return expiredEndDate;
	}

	public String getStatusTip(){
		if(getStatus() == -1){
			return "已过期";
		} else if(getStatus() == 0){
			return "未使用";
		} else if(getStatus() == 1){
			return "已使用";
		} else if(getStatus() == 2){
			return "已失效";
		} else if(getStatus() == 3){
			return "冻结";
		} else {
			return "-";
		}
	}
}
